package hui.zhang.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import hui.zhang.bos.utils.BaseAction;
import hui.zhang.constant.Constants;
import hui.zhang.crm.domain.Customer;
import hui.zhang.domain.base.Area;
import hui.zhang.domain.take_delivery.Order;

@Controller
@ParentPackage("struts-default")
@Namespace("/")
@Scope("prototype")
@SuppressWarnings("all")
public class OrderAction extends BaseAction<Order> {
	
	private String sendAreaInfo;
	private String recAreaInfo;
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}
	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}
	
	
	@Action(value="order_add",results={@Result(name="success",location="index.html",type="redirect"),
				})
	public String add() {
		//截出省市信息封装
		Area sendArea = new Area();
		String[] split = sendAreaInfo.split("/");
		sendArea.setProvince(split[0]);
		sendArea.setCity(split[1]);
		sendArea.setDistrict(split[2]);
//		for (String string : split) {
//			System.out.println(string);
//		}
		
		Area recArea = new Area();
		String[] recAreaData = recAreaInfo.split("/");
		recArea.setProvince(recAreaData[0]);
		recArea.setCity(recAreaData[1]);
		recArea.setDistrict(recAreaData[2]);
//		for (String string : recAreaData) {
//			System.out.println(string);
//		}

		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		
		// 关联当前用户
		Customer customer = (Customer) ServletActionContext.getRequest()
				.getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		
		// 调用WebService将数据传到bos_management，因为要传递 Area Order都要加入注解
		WebClient.create(Constants.BOS_MANAGEMENT_URL+"/bos_management/services/orderService/order")
					.type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	}

}
